/*
 * Main.java
 *
 * Created on June 15, 2007, 2:33 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package Tools;

import java.io.*;
import java.util.*;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

import networkTopo.*;
import InterMessages.*;

import javax.swing.*;

import atag.runtimeForGUI.NodeInfo;

/**
 * @author Sirius
 */
public class SunspotsDeploy extends SwingWorker<BlockingQueue, BlockingQueue> {

    /**
     * Creates a new instance of Main
     */
    private NetworkTopo m_networkTopo;
    private SunspotsDeployStatus m_sunspotsDeployStatus;
    private JTextArea displayScreen;
    String path;
    BlockingQueue bq;
    //int num_nodes;

    public SunspotsDeploy(SunspotsDeployStatus sunspotsDeployStatus, JTextArea textArea, String path, NetworkTopo nettop) {
        this.m_sunspotsDeployStatus = sunspotsDeployStatus;
        this.displayScreen = textArea;
        this.path = path;
        this.bq = new LinkedBlockingQueue();
        this.m_networkTopo = nettop;
    }

    public void setData(NetworkTopo networkTopo) {
        m_networkTopo = networkTopo;

    }


    protected BlockingQueue doInBackground() throws Exception {
        System.out.println("Deployment procedure");
        Properties props = new Properties();
        props.load(new FileInputStream(System.getProperty("user.home") + System.getProperty("file.separator") + "conf" + System.getProperty("file.separator") + "conf.properties"));
        ArrayList<NodeAppDeploymentInfo> NodeFactory;
        NodeFactory = new ArrayList<NodeAppDeploymentInfo>();

        //Add Node Phisical ID -- tasks file directory entry here
        for (NodeInfo ni : this.m_networkTopo.getNodes()) {
            String type = (String) ni.getAttributeByName("type");
            String targetCodeDir = path + "\\node" + ni.getMyId();
            System.out.println(targetCodeDir);
            NodeAppDeploymentInfo Nodei = new NodeAppDeploymentInfo(ni.getPhysicalAddress(), targetCodeDir, type, Integer.toString(ni.getMyId()));//"D:\\Research\\ATaG\\demo\\node1");
            NodeFactory.add(Nodei);
            System.out.println("--" + Nodei.getType() + "--");
        }

        Executer ex = new ExecuterGUI();
        System.out.println("--Execute deploying commands for #" + NodeFactory.size() + " nodes");
        for (int i = 0; i < NodeFactory.size(); i++) {
            String projDir = NodeFactory.get(i).getTasksAppPath();
            File fDir = new File(projDir);
            String type = NodeFactory.get(i).getType();
            System.out.println("Fdir=" + fDir);

            //change the value of "name" in project.xml

            if (!type.equals("android")) {
                String temp = Functions.readFileAsString(fDir + System.getProperty("file.separator") + "nbproject" + System.getProperty("file.separator") + "project.xml");
                Functions.writeFile(fDir + System.getProperty("file.separator") + "nbproject" + System.getProperty("file.separator") + "project.xml", temp.replaceFirst("###", "node" + NodeFactory.get(i).getId() + "_" + type));
                props.load(new FileInputStream(fDir + System.getProperty("file.separator") + props.getProperty("jar_dir")));
            }

            //load the project.properties file in the pros object
            System.out.println("Device = " + type);

            if (type.equals("android")) {
                System.out.println("Android project is created...");
                ex.executeCommand("android update project --path " + fDir + " --target " + "2", fDir, this.m_sunspotsDeployStatus, this.displayScreen, bq);
                ex.executeCommand("ant release", fDir, this.m_sunspotsDeployStatus, this.displayScreen, bq);
            } else if (type.equals("freespot")) {
                System.out.println("freespot project is created...");
                ex.executeCommand("ant deploy -DremoteId=" + NodeFactory.get(i).getPhysicalAddress(), fDir, this.m_sunspotsDeployStatus, this.displayScreen, bq);
            } else {
                System.out.println("j2se project is created...");
                props.setProperty("dist.jar", "${dist.dir}/node" + NodeFactory.get(i).getId() + "-" + type + ".jar");
                //props.setProperty("application.title", "node" + NodeFactory.get(i).getId() + "-" + type);
                props.store(new FileOutputStream(new File(fDir + System.getProperty("file.separator") + props.getProperty("jar_dir"))), "");
                ex.executeCommand("ant jar", fDir, this.m_sunspotsDeployStatus, this.displayScreen, bq);
                //    this.displayScreen.append("jar file has been created under " + fDir);
                //}
                //this.displayScreen
            }
        }
        return null;
    }

    protected void done() {
//        SwingUtilities.invokeLater(new Runnable() {
//            public void run() {
//
//            }
//        });
    }

}

